package esi.t2no1.jobs;

import java.text.Format;
import java.text.SimpleDateFormat;

import org.project.t2rk2.ParseException_Exception;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import esi.t2no1.dao.loadprofile.LoadProfileDAO;
import esi.t2no1.ws.Ws;
import esi.t2no1.ws.model.LoadProfile;

/**
 * 
 * @author Reimo Rebane
 * 
 */
public class ReportLoadProfileJob extends QuartzJobBean {

	private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory
			.getLogger(ReportLoadProfileJob.class);

	@Override
	protected void executeInternal(JobExecutionContext ctx)
			throws JobExecutionException {

		Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		LoadProfileDAO loadProfileDAO = (LoadProfileDAO) ctx.getJobDetail()
				.getJobDataMap().get("loadProfileDao");

		// Send profile shares of T2ES1
		LoadProfile lpes1 = loadProfileDAO.getLatestLoadProfile("T2ES1");
		if (lpes1 != null) {
			System.out.println("Send load profile of T2ES1");
			// Send to T2RK1
			Ws.T2RK1.receiveLoadProfile(Ws.NETWORK_OWNER_ID,
					formatter.format(lpes1.getProfileStartDate()), "666", "666");

			// Send to T2RK2
			try {
				Ws.T2RK2.saveLoadProfile(
						formatter.format(lpes1.getProfileStartDate()), "666",
						"666", Ws.NETWORK_OWNER_ID);
			} catch (ParseException_Exception e) {
				LOG.error("Failed to parse load profile to T2RK2 message format");
			}
		}

		// Send load profile of T2ES2
		LoadProfile lpes2 = loadProfileDAO.getLatestLoadProfile("T2ES2");
		if (lpes2 != null) {
			System.out.println("Send load profile of T2ES2");
			// Send to T2RK1
			Ws.T2RK1.receiveLoadProfile(Ws.NETWORK_OWNER_ID,
					formatter.format(lpes2.getProfileStartDate()), "666", "666");

			// Send to T2RK2
			try {
				Ws.T2RK2.saveLoadProfile(
						formatter.format(lpes2.getProfileStartDate()), "666",
						"666", Ws.NETWORK_OWNER_ID);
			} catch (ParseException_Exception e) {
				LOG.error("Failed to parse load profile to T2RK2 message format");
			}
		}
	}

}
